const App = getApp();
var QQMapWX = require('../../components/qqmap-wx-jssdk.min');
var qqmapsdk;
Page({

  data: {
    // 页面参数
    options: {},
    // 页面元素
    items: {},
    scrollTop: 0,
    imgUrls: [],
    indicatorDots: true, //小点
    indicatorColor: "white", //指示点颜色
    activeColor: "coral", //当前选中的指示点颜色
    autoplay: true, //是否自动轮播
    interval: 5000, //间隔时间
    duration: 3000, //滑动时间
    circular:true,
    url1:'../../images/6.png',
    url2:'../../images/7.png',
    titleBg:'../../images/8.png',
    locationStatus:false,//是否定位过
    city:'正在定位',
    tabList:[],
    newList:[],
    BarHeight: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 当前页面参数
    this.setData({
      options
    });
    if(wx.getStorageSync('city')=='太原市'){
      wx.setStorageSync('city_id', 221)
      wx.setStorageSync('province_id', 220)
    }else if(wx.getStorageSync('city')=='太原'){
      wx.setStorageSync('city_id', 221)
      wx.setStorageSync('province_id', 220)
    }
    // 加载页面数据
    this.getPageData();
    this.getAreta();
    this.getSwiper()
    qqmapsdk = new QQMapWX({
      key: 'LKRBZ-BAG6U-AJSVO-45ALF-3NHL6-3XFHQ'
    });
     // 判断是否定位过,如果没有定位,就执行定位
     if(this.data.locationStatus==false){
      this.dingwei()
      wx.setStorageSync('locationStatus',true)
      this.setData({
        locationStatus:wx.getStorageSync('locationStatus')
      })
    }else{ //若定位过,直接把本地的定位地址赋值
      this.setData({
        city:wx.getStorageSync('city')
      })
    }
    var that = this;
    // 获取通知栏高度
    wx.getSystemInfoAsync({
      success(res) {
        console.log(res.statusBarHeight)
        that.setData({
          BarHeight: res.statusBarHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 更新购物车角标
    // App.setCartTabBadge()
    this.setData({
      city:wx.getStorageSync('city')
    })
  },

  /**
   * 加载页面数据
   */
  getPageData(callback) {
    let _this = this;
    App._get('service_category/home', {
    }, result => {
      // 设置顶部导航栏栏
      // _this.setPageBar(result.data.page);
      _this.setData({
        tabList:result.data.list
      });
      // 回调函数
      typeof callback === 'function' && callback();
    });
  },


  // 获取轮播图
  getSwiper(){
    
    let _this = this;
    App._get('service_category/image', {
    }, result => {
      // 设置顶部导航栏栏
      // _this.setPageBar(result.data.page);
      _this.setData({
        imgUrls:result.data.list
      });
      // 回调函数
    });

  },
  // 咨询列表

  getAreta(){
    let _this = this;
    App._get('article/lists', {
    }, result => {
      // 设置顶部导航栏栏
      // _this.setPageBar(result.data.page);
      _this.setData({
        newList:result.data.list.data
      });
      console.log(_this.data.newList)
    });
  },

  /**
   * 设置顶部导航栏
   */
  setPageBar(page) {
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: page.params.title
    });
    // 设置navbar标题、颜色
    wx.setNavigationBarColor({
      frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
      backgroundColor: page.style.titleBackgroundColor
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    // 获取首页数据
    this.getPageData(function () {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    const _this = this;
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/index/index?" + App.getShareUrlParams()
    };
  },

  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline() {
    const _this = this;
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/index/index?" + App.getShareUrlParams()
    };
  },




  // 就医陪诊
  getVisit(){
    wx.navigateTo({
      url: '/pages/visit/index',
    })
  },

  // 井冈区
  ToHotel(e){
    wx.navigateTo({
      url: '/pages/order_form/index?Typename='+e.currentTarget.dataset.name+'&category_id='+e.currentTarget.dataset.category_id+'&typeName='+'1',
    })
  },
  onEmpty(){
    wx.showToast({
      title:"暂未开通",
      icon:'none'
    })
   
    // wx.showModal({
    //   title:'暂未开通',
    //   cancelColor: 'cancelColor',
    // })
  },
//资讯详情
onTodetail(e){
  wx.navigateTo({
    url: '/pages/index/dwtail/index?id='+e.currentTarget.dataset.id,
  })
},

// getCity
getCity(){
  wx.navigateTo({
    url: '/packageA/pages/city/index',
  })
},


  // 打开设置
  openConfirm(){
    wx.showModal({
      title: '请求授权当前位置',
      content: '需要获取您的地理位置，请确认授权',
      success:(res=>{
        if(res.confirm){
          wx.openSetting();// 打开地图权限设置
        }else if(res.cancel){
          wx.showToast({
            title: '你拒绝了授权，无法获得周边信息',
            icon: 'none',
            duration: 1000                            
          })
        }
      }) 
    })
  },
  // 定位
  dingwei(){
    var that = this 
    qqmapsdk.reverseGeocoder({
      success: function(res) {
        console.log(res);
        wx.setStorageSync('longitude',res.result.location.lng);
        wx.setStorageSync('latitude',res.result.location.lat);
        wx.setStorageSync('city',res.result.address_component.city)
        wx.setStorageSync('province',res.result.address_component.province)
        that.setData({
          city:wx.getStorageSync('city'),
          province:wx.getStorageSync('province')
        })
        if(that.data.city=='太原市'){
          wx.setStorageSync('city_id', 221)
          wx.setStorageSync('province_id', 220)
        }else if(that.data.city=='太原'){
          wx.setStorageSync('city_id', 221)
          wx.setStorageSync('province_id', 220)
        }
      }
     })
  },
});